//
//  OO_OrderViewController.m
//  ShopProject
//
//  Created by macbookpro on 2018/9/28.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "OO_OrderViewController.h"
#import "OO_AdressMenageViewController.h"
#import "OO_OrderCell.h"
#import "OO_PayViewController.h"
@interface OO_OrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *addressView;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *phoneLable;
@property (weak, nonatomic) IBOutlet UILabel *addressLable;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (weak, nonatomic) IBOutlet UIButton *submitButton;

@property (weak, nonatomic) IBOutlet UILabel *goodsMoneyLable;
@property (weak, nonatomic) IBOutlet UILabel *postMoneyLable;
@property (weak, nonatomic) IBOutlet UILabel *allMoneyLable;
@property (nonatomic,assign) CGFloat allMoney;
@property (nonatomic,strong) OO_AddressModel *addressModel;
@end

@implementation OO_OrderViewController
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    
    return _dataSource;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.submitButton.backgroundColor  = MainColor;
    self.title = @"订单详情" ;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"OO_OrderCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"OO_OrderCell"];
    self.addressView.hidden = YES;
    /*立即购买*/
    if (self.orderModel) {
        [self.dataSource addObject:self.orderModel];
        [self.tableView reloadData];
    }
    /*购物车*/
    else{
        
        [self.dataSource addObjectsFromArray:self.orderArr];
        [self.tableView reloadData];
        
    }
    CGFloat goodsMoney = 0.0f;
    for (OO_OrderModel *model in self.dataSource) {
        
        goodsMoney += [model.price doubleValue] * model.goodsNumber;
        
    }
    self.goodsMoneyLable.text = [NSString stringWithFormat:@"¥%.2lf",goodsMoney];
    self.postMoneyLable.text = [NSString stringWithFormat:@"¥%.2lf",POSTMONEY];
    self.allMoneyLable.text = [NSString stringWithFormat:@"¥%.2lf",goodsMoney + POSTMONEY];
    
    self.allMoney = goodsMoney ;
}
- (IBAction)payButtonPress:(UIButton *)sender {
    
    
    if (!self.addressModel) {
        [MBProgressHUD showTipMessageInWindow:@"请选择收货地址"];
        return;
    }
    
    WeakSelf
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSMutableArray *orderArr = [NSMutableArray new];
        
        /*立即购买*/
        if (self.orderModel) {
            
            JQFMDB *db = [JQFMDB shareDatabase];
            NSDate *date=[NSDate date];
            NSDateFormatter *format1=[[NSDateFormatter alloc] init];
            [format1 setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
            NSString *dateStr;
            dateStr=[format1 stringFromDate:date];
            self.orderModel.time = dateStr;
            [db jq_insertTable:TOBEPAY dicOrModel:self.orderModel];
            [orderArr addObject:self.orderModel];
        }
        /*购物车*/
        else{
            
            JQFMDB *db = [JQFMDB shareDatabase];
            NSDate *date=[NSDate date];
            NSDateFormatter *format1=[[NSDateFormatter alloc] init];
            [format1 setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
            NSString *dateStr;
            dateStr=[format1 stringFromDate:date];
            NSMutableArray *orderArr = [NSMutableArray new];
            for (OO_OrderModel *model in self.dataSource) {
                
                
                model.time = dateStr;
                [db jq_deleteTable:SHOPCARTABLE whereFormat:@"where ID = '%@'",model.ID];
                [orderArr addObject:model];
            }
            [db jq_insertTable:TOBEPAY dicOrModelArray:orderArr];
            [orderArr addObjectsFromArray:orderArr];
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            OO_PayViewController *vc = [OO_PayViewController new];
            vc.allMoney = weakSelf.allMoney;
            vc.orderArr = orderArr;
            LEINavigationController *nav = [[LEINavigationController alloc] initWithRootViewController:vc];
            
            [weakSelf.navigationController presentViewController:nav animated:1 completion:^{
                [weakSelf.navigationController popToRootViewControllerAnimated:YES];
            }];
        });
        
        
    });
    
    


    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
- (IBAction)chooseAddressButtonPress:(UIButton *)sender {
    OO_AdressMenageViewController *vc = [OO_AdressMenageViewController new];
    WeakSelf
    vc.selectedAddress = ^(OO_AddressModel *addressModel) {
        weakSelf.addressModel = addressModel;
        
    };
    [self.navigationController pushViewController:vc animated:1];
    
}
-(void)setAddressModel:(OO_AddressModel *)addressModel{
    
    _addressModel = addressModel;
    self.nameLable.text = addressModel.peopleName;
    self.phoneLable.text = addressModel.phone;
    self.addressLable.text = [NSString stringWithFormat:@"%@%@",addressModel.city,addressModel.adress];
    self.addressView.hidden = NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OO_OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OO_OrderCell"];
    cell.model = self.dataSource[indexPath.row];
    return cell;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120.0f;
    
    
}
@end
